import { Layout, Menu as AntMenu } from "antd";
// import classNames from "classnames";
import * as React from "react";
import MenuRender from "./MenuRender";
import { MenuProps } from "antd/lib/menu";
import styles from "./style.module.less";
import {withRouter} from "react-router-dom";
import {connect} from "react-redux";
import Selector from "../../reselector";
const { Sider } = Layout;

interface State {
  collapsed: boolean;
  active: string;
}

class Menu extends React.Component<any, State> {
  public renderedMenu: any;
  constructor(props: any) {
    super(props);
    this.state = {
      collapsed: false,
      active: "home"
    };
    // console.log(props.menusGetter(props.match.url))
    this.renderedMenu = props.menusGetter(props.match.url).map(MenuRender) || null;
  }
  /*
  public onOpenChange = (openKeys: string[]) => {
    this.props.handleModuleSelect(openKeys);
  };*/

  public render() {
    const { menuSelection } = this.props;
    // console.log(this.props.match)
    const menuProps: MenuProps = {
      theme: "light",
      // defaultSelectedKeys: [this.state.active],
      // onOpenChange:this.onOpenChange,
      selectedKeys: [menuSelection],
      mode: "inline",
      className: "top-nav-menu"
    };
    /*if(!this.props.collapsed){
        menuProps.openKeys = [...openKeys]
    }*/
    return (
      <Sider
        theme="light"
        collapsed={this.props.collapsed}
        style={{ height: "100%" }}
        trigger={null}
        collapsible={true}
        breakpoint="lg"
        width={200}
        className={styles.sider}
      >
        <div className={styles.logo} id="logo">
          <h1>RN包管理系统</h1>
        </div>
        <AntMenu {...menuProps}>
          {this.renderedMenu && this.renderedMenu}
        </AntMenu>
      </Sider>
    );
  }
}

const mapStateToProps = (store: any, ownProps: any) => {
  return {
      menuSelection: Selector.getMenuSelection(store),
      menusGetter: Selector.getPathMenu(store)
  }
};

export default connect(mapStateToProps)(withRouter(Menu));

// export default connect(withRouter(Menu);
